//
//  CYMyReleaseDetail.m
//  CreditAddressBook
//
//  Created by Lee on 15/7/31.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYMyReleaseDetail.h"
#import "CYMyReleaseDetailTVC.h"
#import "CYCreditTool.h"
#import "CYDefaultRecord.h"
#import "CYReleaseVC.h"
#import "MBProgressHUD+MJ.h"
#import "UIInfomationView.h"
#import "CYMyRelease.h"
#import "CYJumpsTool.h"
#import "CYLoginHttpTool.h"

@interface CYMyReleaseDetail ()

@property (weak, nonatomic) IBOutlet UIView *submitView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *submitViewW;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (weak, nonatomic) IBOutlet UIButton *editBtn;
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;
@end

@implementation CYMyReleaseDetail

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubmitBtnShow];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)setupSubmitBtnShow {
    int status = self.record.status.intValue;
    if (status == 0) {
        self.submitBtn.enabled = YES;
        self.editBtn.enabled = YES;
        self.deleteBtn.enabled = YES;
    } else if (status == 1) {
        self.submitBtn.enabled = NO;
        self.editBtn.enabled = NO;
        self.deleteBtn.enabled = YES;
    } else if (status == -1) {
        self.submitBtn.enabled = NO;
        self.editBtn.enabled = NO;
        self.deleteBtn.enabled = NO;
    }
}

/**
 *  撤销
 */
- (IBAction)submitCLick:(id)sender {
    [UIInfomationView showAlertViewWithTitle:nil message:@"确认撤销本条违约记录吗?" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] clickAtIndex:^(NSInteger buttonIndex) {
        if (buttonIndex == CYIndexSecond) {
            [MBProgressHUD showMessage:@"撤销中" toView:self.view];
            [CYCreditTool cancelDefaultRecordWithRecordId:self.record.defaultRecordId success:^(NSString *message) {
                
                [MBProgressHUD hideHUDForView:self.view];
                [MBProgressHUD showSuccess:message];
                
                //跳转到我的需要通缉首页
//                CYMyRelease *release = [self.storyboard instantiateViewControllerWithIdentifier:@"CYMyRelease"];
//                release.backStyle = CYBackStyleDouble;
                [CYJumpsTool jumpToMyWithController:self backStyle:CYBackStyleDouble];
            } failure:^(NSError *error) {
                [MBProgressHUD hideHUDForView:self.view];
            }];
        }
    }];
}

/**
 *  编辑
 */
- (IBAction)editClick:(id)sender {
    CYUser *user = [CYLoginHttpTool userData].user;
    [MBProgressHUD showMessage:@"加载中" toView:self.view];
    [CYCreditTool editDefaultRecordWithParams:@{@"id": self.record.defaultRecordId, @"userId": user.userId, @"token": user.token} success:^(CYDefaultRecord *defr) {
        [MBProgressHUD hideHUDForView:self.view];
        CYReleaseVC *release = [self.storyboard instantiateViewControllerWithIdentifier:@"CYReleaseVC"];
        release.record = defr;
        [self.navigationController pushViewController:release animated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
    }];
}

/**
 *  删除
 */

- (IBAction)deleteClick:(id)sender {
    CYWeakObject(weakSelf, self);
    CYUser *user = [CYLoginHttpTool userData].user;
    [UIInfomationView showAlertViewWithTitle:nil message:@"确认删除该条记录？" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] clickAtIndex:^(NSInteger buttonIndex) {
        if (buttonIndex == CYIndexSecond) {
            [MBProgressHUD showMessage:@"操作中" toView:self.view];
            [CYCreditTool deleteDefaultRecordWithParams:@{@"id": self.record.defaultRecordId, @"userId": user.userId, @"token": user.token} success:^(NSString *message) {
                [MBProgressHUD hideHUDForView:self.view];
                [MBProgressHUD showSuccess:message];
                
                if ([weakSelf.delegate respondsToSelector:@selector(deleteReleaseViewController:)]) {
                    [weakSelf.delegate deleteReleaseViewController:weakSelf];
                }
                
                [weakSelf.navigationController popViewControllerAnimated:YES];
            } failure:^(NSError *error) {
                [MBProgressHUD hideHUDForView:self.view];
                
            }];
        }
    }];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    CYMyReleaseDetailTVC *controller = segue.destinationViewController;
    controller.record = self.record;
    
}

@end
